package imserver.dao;

import java.io.Serializable;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;

import imserver.beans.MessageInfo;
import imserver.common.util.SerializeUtil;

/**
 * 消息相关的Dao，使用Redis来进行存储
 * @author zhanghongbin01
 *
 */
public class MessageDao {
	
	@Resource
	private  RedisTemplate<Serializable, Serializable>  redisTemplate;
	
	/**
	 * 持久化消息
	 * @param messageInfo
	 */
	public void save(final MessageInfo messageInfo){
		redisTemplate.execute(new RedisCallback<MessageInfo>(){
			@Override
			public MessageInfo doInRedis(RedisConnection conn) throws DataAccessException {
				conn.set(SerializeUtil.serialize(messageInfo.getTo()), SerializeUtil.serialize(messageInfo));
				return null;
			}});
	}
}
